package com.sirun.cn.fca.module.main.home.temp.status;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sirun.cn.fca.R;
import com.tencent.cloud.iov.block.BaseBlockLayout;
import com.tencent.cloud.iov.block.Block;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.ViewStateUtils;
import com.tencent.cloud.uikit.ui.drawable.SectorDrawable;

/**
 * @author showxiao
 * @date 2018/8/10
 */
@Block
public class HomeCarStatusLayout extends BaseBlockLayout<IHomeCarStatusItem> {

    private static final int FIXED_WIDTH = ResourcesUtils.getDimensionPixelOffset(
            R.dimen.home_car_status_width_prototype
    );

    private static final int FIXED_HEIGHT = ResourcesUtils.getDimensionPixelOffset(
            R.dimen.home_car_status_height_prototype
    );

    private static final float HEIGHT_WIDTH_RATIO = ((float) FIXED_HEIGHT) / FIXED_WIDTH;

    private LinearLayout mCarStatusContainer;
    private ImageView mCarStatusIcon;
    private TextView mCarStatusText;

    private TextView mMileageNumberTextView;
    private SectorDrawable mMileageLeftHintDrawable;

    @Override
    protected View createView(ViewGroup parent) {
        RelativeLayout view = (RelativeLayout) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_car_status, parent, false);

        mCarStatusContainer = view.findViewById(R.id.ll_car_status);
        mCarStatusIcon = mCarStatusContainer.findViewById(R.id.iv_car_status_icon);
        mCarStatusText = mCarStatusContainer.findViewById(R.id.tv_car_status_text);
        ViewStateUtils.setUpViewPressState(mCarStatusContainer);

//        ImageView mileageLeftHintImageView = view.findViewById(R.id.iv_car_mileage_hint);
//        LayerDrawable hintDrawable = getMileageLeftDrawable();
//        mileageLeftHintImageView.setBackground(hintDrawable);

//        float originRatio = ((float) hintDrawable.getIntrinsicHeight()) / hintDrawable.getIntrinsicWidth();
//        ViewGroup.LayoutParams lp = mileageLeftHintImageView.getLayoutParams();
//        lp.width = parent.getWidth() - 140;
//        lp.height = (int) (lp.width * originRatio);

        mMileageNumberTextView = view.findViewById(R.id.tv_car_mileage_number);

        setUpMargins(view, parent.getWidth() - 140);

        return view;
    }

    @NonNull
    private LayerDrawable getMileageLeftDrawable() {
        Drawable bgDrawable = ResourcesUtils.getDrawable(R.drawable.ic_home_car_status_oil_bg);
        mMileageLeftHintDrawable = new SectorDrawable(
                ResourcesUtils.getDrawable(R.drawable.ic_home_car_status_oil)
        );

        Drawable hintDrawable = ResourcesUtils.getDrawable(R.drawable.ic_home_car_status_oil);
        mMileageLeftHintDrawable.setCenterOffsetY(ResourcesUtils.getDimensionPixelOffset
                (R.dimen.home_car_status_oil_center_offset_y));
        return new LayerDrawable(new Drawable[]{bgDrawable, hintDrawable});
    }

    private void setUpMargins(RelativeLayout rootView, int parentWidth) {
        float scaleRatio = ((float) parentWidth) / FIXED_WIDTH;

        if (scaleRatio == 0.0f) {
            return;
        }

        int childCount = rootView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = rootView.getChildAt(i);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) child.getLayoutParams();
            params.topMargin *= scaleRatio;
        }

        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) rootView.getLayoutParams();
        lp.topMargin *= scaleRatio;
        lp.bottomMargin *= scaleRatio;
        lp.width = parentWidth;
        lp.height = (int) (parentWidth * HEIGHT_WIDTH_RATIO);
    }

    @Override
    protected void bindView(IHomeCarStatusItem item, int position) {
        mCarStatusIcon.setImageDrawable(item.getCarStatusIcon());
        mCarStatusText.setText(item.getCarStatusText());
        mMileageNumberTextView.setText(item.getMileageImageString());
//        mMileageLeftHintDrawable.setLevel((int) (item.getMileageLeftRatio() * SectorDrawable.MAX_LEVEL));
    }

//    @Override
//    protected void bindAction(IHomeCarStatusItem item, int position, IActionListener actionListener) {
//        mCarStatusContainer.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getActionListener().onAction(ICarStatusAction.ACTION_CLICK_CAR_STATUS);
//            }
//        });
//    }
}
